<?php

use yii\helpers\Html;
use yii\grid\GridView;
use common\business\EhrHelper;
use yii\helpers\ArrayHelper;

/* @var $this yii\web\View */
/* @var $searchModel common\models\EmployeeSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Employees');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="employee-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a(Yii::t('app', 'Create Employee'), ['create'], ['class' => 'btn btn-success']) ?>
        <?= Html::a(Yii::t('app', 'Sync Employee from HR'), ['sync-from-hr'], ['class' => 'btn btn-primary', 'target'=>'_blank']) ?>
        <?= Html::a(Yii::t('app', 'Export To CSV'), ['export-to-csv'], ['class' => 'btn btn-primary', 'target'=>'_blank']) ?>
        <?= Html::a(Yii::t('app', 'Import From CSV'), ['import'], ['class' => 'btn btn-primary', 'target'=>'_blank']) ?>
        <?= Html::a(Yii::t('app', 'Sync Employee To Qywx'), ['sync-to-qywx'], ['class' => 'btn btn-primary', 'target'=>'_blank']) ?>
        <?= Html::a(Yii::t('app', 'Sync Employee Photo'), ['sync-photo'], ['class' => 'btn btn-primary', 'target'=>'_blank']) ?>
    </p>

    <?php \yii\widgets\Pjax::begin(['id' => 'employees']) ?>
    <div class="box box-default">
        <div class="box-header with-border">
            <h3 class="box-title"><?= $this->title?></h3>
            <div class="box-tools pull-right">
                <?= Html::button(Yii::t('app', 'Delete'), [
                    'class' => 'btn btn-danger btn-sm',
                    'onclick' => 'on_delete_btn_clicked()'
                ]) ?>
                <?= Html::a(Yii::t('app', 'Create'), ['create'], ['class' => 'btn btn-primary btn-sm']) ?>
            </div>
        </div>
        <div class="box-body">
            <div class="table-responsive">
                <?php
                try {
                    $gv = GridView::widget([
                        'id' => 'employeeGrid',
                        'tableOptions' => ['class' => 'table table-condensed table-bordered table-striped table-hover'],
                        'pager' => [
//            'class'          => GoPager::className(),
                            'nextPageLabel'  => Yii::t('app', 'Next Page'),
                            'prevPageLabel'  => Yii::t('app', 'Previous Page'),
                            'firstPageLabel' => Yii::t('app', 'First Page'),
                            'lastPageLabel'  => Yii::t('app', 'Last Page'),
                            'options' => [
                                'class' => 'pagination pagination-sm m-t-none m-b-none',
                            ],
                        ],
                        'dataProvider' => $dataProvider,
                        'filterModel' => $searchModel,
                        'columns' => [
                            ['class' => 'yii\grid\ActionColumn'],
//                [
//                    'class' => 'yii\grid\SerialColumn',
//                    'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 25px;'],
//                ],
                            [
                                'class' => 'yii\grid\CheckboxColumn',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 30px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 30px;'],
                                'name' => 'id',
                            ],
                            [
                                'label' => Yii::t('app', 'Id'),
                                'attribute' => 'id',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'label' => Yii::t('app', 'Name'),
                                'attribute' => 'name',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'attribute' => 'number',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'label' => Yii::t('app', 'Gender'),
                                'attribute' => 'gender',
                                'value' => function ($model, $key, $index, $column) {
                                    return EhrHelper::getGenderByIntValue($model['gender']);
                                },
                                'filter' => EhrHelper::getGender_map(),
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 100px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 100px;'],
                            ],
                            [
                                'label' => Yii::t('app', 'Birthday'),
                                'attribute' => 'birthday',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'label' => Yii::t('app', 'Department Name'),
                                'attribute' => 'department_name',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
//                [
//                    'label' => Yii::t('app', 'Email'),
//                    'attribute' => 'email',
//                    'format' => 'email',
//                    'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
//                    'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
//                ],
                            [
                                'label' => Yii::t('app', 'Labor Relation State'),
                                'attribute' => 'labor_relation_state',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'label' => Yii::t('app', 'Position Name'),
                                'attribute' => 'position_name',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
//                [
//                    'label' => Yii::t('app', 'Mobile'),
//                    'attribute' => 'mobile',
//                    'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
//                    'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
//                ],
//                [
//                    'label' => Yii::t('app', 'Id Card No'),
//                    'attribute' => 'id_card_num',
//                    'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
//                    'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
//                ],
//                [
//                    'label' => Yii::t('app', 'Native Place'),
//                    'attribute' => 'native_place',
//                    'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
//                    'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
//                ],
                            [
                                'label' => Yii::t('app', 'Blood Type'),
                                'attribute' => 'blood_type',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'label' => Yii::t('app', 'Wed Status'),
                                'attribute' => 'wed_status',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'label' => Yii::t('app', 'Nationality'),
                                'attribute' => 'nationality',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'label' => Yii::t('app', 'Folk'),
                                'attribute' => 'folk',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                            [
                                'label' => Yii::t('app', 'Hklx'),
                                'attribute' => 'hklx',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
//             'hr_id',
                            [
                                'label' => Yii::t('app', 'Hr Sync Status'),
                                'attribute' => 'hr_status',
                                'value' => function ($model, $key, $index, $column) {
                                    return EhrHelper::getSyncStatusByIntValue($model['hr_status']);
                                },
                                'filter' => EhrHelper::getSyncStatus_map(),
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 100px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 100px;'],
                            ],
//             'mdm_id',
                            [
                                'label' => Yii::t('app', 'Mdm Sync Status'),
                                'attribute' => 'mdm_status',
                                'value' => function ($model, $key, $index, $column) {
                                    return EhrHelper::getSyncStatusByIntValue($model['mdm_status']);
                                },
                                'filter' => EhrHelper::getSyncStatus_map(),
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 100px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 100px;'],
                            ],
                            // 'memo',
                            // 'created_by',
                            // 'created_at',
                            // 'updated_by',
//                'updated_at',
                            [
                                'label' => Yii::t('app', 'Updated At'),
                                'attribute' => 'updated_at',
                                'headerOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                                'contentOptions' => ['class' => 'vertical-middle text-center', 'style' => 'width: 80px;'],
                            ],
                        ],
                    ]);
                } catch (Exception $e) {
                }
                ?>
                <?= $gv ?>
            </div>
        </div>
        <div class="box-footer with-border">
        </div>
    </div>
    <?php \yii\widgets\Pjax::end() ?>

</div>
<script>
    <?php $this->beginBlock('js_running')?>
    $(function () {
        $('.search-options').click(function () {
            if ($('.search-options-content').is(':visible')) {
                $(this).find('i').addClass('fa-arrow-circle-up').removeClass('fa-arrow-circle-down');
                $('.search-options-content').slideUp();
            } else {
                $(this).find('i').addClass('fa-arrow-circle-down').removeClass('fa-arrow-circle-up');
                $('.search-options-content').slideDown();
            }
        });

        $('.reset').bind('click', function () {
            $('#qywxUserGrid').find(':text').each(function () {
                $(this).val('');
            });
            $('#qywxUserGrid').find('select').each(function () {
                $(this).val('');
                $(this).change();
            });
            return true;
        });
    });

    function on_delete_btn_clicked() {
        $keys = $('#employeeGrid').yiiGridView('getSelectedRows');
        if ($keys.length === 0){
            WarningDlg.alert('您没有选择任何项噢！');
            return;
        }

        $postData = 'ids4Delete=' + $keys.join();
//        alert($postData);

        krajeeDialog.confirm("您确定要删除这些项吗?", function (result) {
            if (result) {
                $.ajax({
                    type: 'post',
                    url: 'batch-delete',
                    data: $postData,
                    success: function(data){
                        var result = JSON.parse(data);
                        if (result.code === 200){
                            SuccessDlg.alert(result.message);
                            $.pjax.reload({container:"#employees"});
                        }
                        else {
                            ErrorDlg.alert(result.message);
                        }
                    },
                });
            }
        });
    }
    <?php $this->endBlock()?>
</script>
<?php
$this->registerJs($this->blocks['js_running'], \yii\web\View::POS_END)
?>
